using System;
using System.ComponentModel;
using UnityEngine.Playables;
using UnityEngine.Timeline;

namespace UnityEngine.Recorder.Timeline
{
	[DisplayName("Recorder Clip")]
	public class RecorderClip : PlayableAsset, ITimelineClipAsset
	{
		public delegate void RecordingClipDoneDelegate(RecorderClip clip);

		public static RecordingClipDoneDelegate OnClipDone;

		[SerializeField]
		public RecorderSettings m_Settings;

		public Type recorderType => (!(m_Settings == null)) ? m_Settings.recorderType : null;

		public ClipCaps clipCaps => ClipCaps.None;

		public override Playable CreatePlayable(PlayableGraph graph, GameObject owner)
		{
			ScriptPlayable<RecorderPlayableBehaviour> playable = ScriptPlayable<RecorderPlayableBehaviour>.Create(graph);
			RecorderPlayableBehaviour behaviour = playable.GetBehaviour();
			if (recorderType != null && UnityHelpers.IsPlaying())
			{
				behaviour.session = new RecordingSession
				{
					m_Recorder = RecordersInventory.GenerateNewRecorder(recorderType, m_Settings),
					m_RecorderGO = SceneHook.HookupRecorder()
				};
				behaviour.OnEnd = delegate
				{
					try
					{
						if (OnClipDone != null)
						{
							OnClipDone(this);
						}
					}
					catch (Exception ex)
					{
						UnityEngine.Debug.Log("OnClipDone call back generated an exception: " + ex.Message);
						UnityEngine.Debug.LogException(ex);
					}
				};
			}
			return playable;
		}

		public virtual void OnDestroy()
		{
			UnityHelpers.Destroy(m_Settings, allowDestroyingAssets: true);
		}
	}
}
